package LeetcodeTest_forty_two;
//19. 删除链表的倒数第 N 个结点
public class removeNthFromEnd
{
    public static void main(String[] args)
    {
        ListNode head=new ListNode(0);
        head=Solutions.removeNthFromEnd(head,4);
    }

}
class Solutions
{
    public static ListNode removeNthFromEnd(ListNode head, int n)
    {
        ListNode p1=head;
        ListNode p2=head;
        ListNode p3=head;
        int count=0;
        while (count<n-1)
        {
            p2=p2.next;
            count++;
        }
        if(p2.next==null)
            return p1.next;
        p2=p2.next;
        p1=p1.next;
        while (p2.next!=null)
        {
            p3=p3.next;
            p2=p2.next;
            p1=p1.next;
        }
        p3.next=p1.next;
        return head;
    }
}
class ListNode
{
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}